package com.topvision.myviewtest.gscview.ball;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * @author lucy
 * @date 2019-01-29 11:23
 * @description //TODO
 */

public class RunView extends View {
    /**
     * 时间流
     */
    private ValueAnimator mValueAnimator;
    private Ball mBall;
    private Paint mPaint;
    private Paint mBoxPaint;

    /**
     * 坐标系
     */
    private Point mCoordinate;

    private float defaultR = 40;
    private int defaultColor = Color.RED;
    private float defaultVX = 10;
    private float defaultVY = 0;
    private float defaultAY = 0;
    private float defaultAX = 0;
    private Point mScreenPoint;

    public RunView(Context context) {
        this(context, null);
    }

    public RunView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RunView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mScreenPoint = UIHelper.getScreenSize(getContext());
        mCoordinate = new Point(mScreenPoint.x / 2, mScreenPoint.y / 2);
        mBall = new Ball();

        mBall.r = defaultR;
        mBall.vX = defaultVX;
        mBall.vY = defaultVY;
        mBall.color = defaultColor;
        mBall.aX = defaultAX;
        mBall.aY = defaultAY;

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        mBoxPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBoxPaint.setStrokeWidth(2);
        mBoxPaint.setStyle(Paint.Style.STROKE);
        mBoxPaint.setColor(Color.BLACK);

        mValueAnimator = ValueAnimator.ofFloat(0, 1);
        mValueAnimator.setRepeatCount(-1);
        mValueAnimator.setDuration(1000);
        mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                updateBall();
                invalidate();
            }
        });
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        canvas.translate(mCoordinate.x, mCoordinate.y);
        canvas.drawRect(-800, -400, 800, 400, mBoxPaint);
        canvas.drawLine(-800, mBall.r, 800, mBall.r, mBoxPaint);
        drawBall(canvas);
    }

    private void drawBall(Canvas canvas) {
        mPaint.setColor(mBall.color);
        canvas.drawCircle(mBall.x, mBall.y, mBall.r, mPaint);
    }

    private void updateBall() {
        mBall.x += mBall.vX;
        if (mBall.x > (800 - mBall.r)
                || mBall.x < -(800 - mBall.r)) {
            mBall.vX = -mBall.vX;
            mBall.color = UIHelper.randomColor();
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mValueAnimator.start();
                break;
            case MotionEvent.ACTION_UP:
                mValueAnimator.pause();
                break;
            default:
                break;
        }
        return true;
    }
}
